/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.pa165.bookingmanager.restclient;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientHandlerException;
import com.sun.jersey.api.client.GenericType;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;
import com.sun.jersey.api.client.filter.HTTPBasicAuthFilter;
import com.sun.jersey.api.json.JSONConfiguration;
import cz.muni.fi.pa165.bookingmanager.dto.HotelDTO;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.swing.JOptionPane;

import javax.ws.rs.core.MediaType;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jettison.json.JSONObject;

/**
 *
 * @author Daniel
 */
public class HotelRESTClient {
    
    private String url;
    private Client client;
    
    private WebResource webResource;
    private ObjectMapper mapper = new ObjectMapper();
    
    
    public HotelRESTClient(){        
        ClientConfig config = new DefaultClientConfig();
        client = Client.create(config); 
        client.addFilter(new HTTPBasicAuthFilter("rest", "rest"));
        webResource = client.resource(URIHelper.getURL());
    }
    
    public void createHotel(HotelDTO hotel){      
        try{webResource.path("hotel")
                   .accept(MediaType.APPLICATION_JSON)
                   .post(hotel);
        }
        catch(ClientHandlerException ex){
            JOptionPane.showMessageDialog(null,"server is unavailable");
            System.exit(-1);           
        }
    }
    
    public void deleteHotel(HotelDTO hotel){
        try{webResource.path("hotel")
                   .path("delete")
                   .accept(MediaType.APPLICATION_JSON)
                   .post(hotel);
        }
        catch(ClientHandlerException ex){
            JOptionPane.showMessageDialog(null,"server is unavailable");
            System.exit(-1);           
        }
    }
    
    public void editHotel(HotelDTO hotel){
        try{webResource.path("hotel")
                   .type(MediaType.APPLICATION_JSON)
                   .put(hotel);}
        catch(ClientHandlerException ex){
            JOptionPane.showMessageDialog(null,"server is unavailable");
            System.exit(-1);           
        }
    }
    
    public List<HotelDTO> findHotelsByName(String name){
        List<HotelDTO> hotels = new ArrayList<HotelDTO>();
        try{
        webResource.path("hotel")
                                           .path("allByName-{name}")
                                           .type(MediaType.APPLICATION_JSON)
                                           .get(new GenericType<List<HotelDTO>>(){}); 
        }catch(ClientHandlerException ex){
            JOptionPane.showMessageDialog(null,"server is unavailable");
            System.exit(-1);           
        }
        return Collections.unmodifiableList(hotels);
    }
    
    public List<HotelDTO> findHotelsByCity(String city){
        List<HotelDTO> hotels = new ArrayList<HotelDTO>();
        try{
        webResource.path("hotel")
                                           .path("allByCity-{city}")
                                           .accept(MediaType.APPLICATION_JSON)
                                           .get(new GenericType<List<HotelDTO>>(){}); 
        }
        catch(ClientHandlerException ex){
            JOptionPane.showMessageDialog(null,"server is unavailable");
            System.exit(-1);           
        }
        return Collections.unmodifiableList(hotels);
    }
    
    public List<HotelDTO> findAllHotels(){
        List<HotelDTO> hotels = new ArrayList<HotelDTO>();
        try{        
        hotels=webResource.path("hotel")
                                           .path("all")
                                           .type(MediaType.APPLICATION_JSON)
                                           .get(new GenericType<List<HotelDTO>>(){});
        }
        catch(ClientHandlerException ex){
            JOptionPane.showMessageDialog(null,"server is unavailable");
            System.exit(-1);           
        }             
        return Collections.unmodifiableList(hotels);
    }
    
    public HotelDTO getHotelById(Long id) {
        return webResource.path("customer").path("{id}").accept(MediaType.APPLICATION_JSON)
                .get(HotelDTO.class);
    }
    
}
